Page({
  data: {
    pmtype: 0, // 菜单层级
    pmtypeId: '', // 一级菜单选择的id
    index: '', // 打印类型第几个
    printTypeList: [], // 打印类型列表
    typeListData: [], // 二级菜单数据data
    selectObj: {},
    version: '',
    showAdvert: false,
  },
  onLoad() {
    wx.showToast({
      title: '请选择文件类型',
      icon: 'none',
      duration: 3000,
    })
    this.getPrintType()
  },
  closeAdvert(e) {
    this.setData({
      showAdvert: e.detail,
    })
  },
  /**
   * @see 获取绩点类型、排名类型
   */
  getType() {
    const { printTypeList, index } = this.data
    wx.showLoading({
      title: '类型选项加载中...',
      mask: true,
    })
    const request = {
      fileProperty: printTypeList[index].vcPrintTypeId,
    }
    wx.$API['workService/getType'](request)
      .then((result) => {
        const { data } = result
        wx.hideLoading()
        if (data.length) {
          // 这里要做版本区别
          if (result.version) {
            // 这是新版
            this.setData({
              version: result.version,
              typeListData: data,
            })
          } else {
            if (data[0].typeList && data[0].typeList.length) {
              // 存在二级菜单
              data.forEach((item) => {
                item.search = '0'
              })
              this.setData({
                typeListData: data,
                pmtype: 2,
              })
            } else {
              // 存在一级菜单
              this.setData({
                typeListData: data,
                pmtype: 1,
              })
            }
          }
        } else {
          // 如果没有一级或者二级菜单直接跳转到预览图
          // 跳转之前将页面中的二级菜单数据清除,以免造成返回后二级菜单仍然存在的bug
          this.setData({
            pmtype: 0,
            pmtypeId: '',
            typeListData: [],
          })
          const param = { filePropertyType: 0 }
          const currentPrintType = encodeURIComponent(
            JSON.stringify(printTypeList[index])
          )
          const paramString = encodeURIComponent(JSON.stringify(param))

          const url = `/pages/workService/components/pdfImage/index?currentPrintType=${currentPrintType}&paramString=${paramString}`
          wx.navigateTo({
            url,
          })
        }
      })
      .catch((error) => {
        console.log(error)
        wx.hideLoading()
        wx.showToast({
          title: error,
          icon: 'none',
        })
      })
  },
  /**
   * @see 点击绩点类型、排名类型
   */
  handleType(e) {
    console.log(e)
    let { currentTarget, detail } = e
    console.log(currentTarget, detail)
    if (this.data.pmtype === 1) {
      this.setData({
        pmtypeId: detail.value,
      })
    } else {
      const index = currentTarget.dataset.index
      const value = detail.value
      const typeListData = [...this.data.typeListData]
      typeListData[index].search = value
      this.setData({
        typeListData,
      })
    }
  },
  /**
   * @see 点击跳转生成预览图
   */
  goPreview() {
    const { printTypeList, typeListData, index, pmtype, pmtypeId } = this.data
    if (index === '') {
      wx.showToast({
        title: '请选择文件类型',
        icon: 'none',
      })
      return
    }
    wx.showLoading({ mask: true })
    const param = { filePropertyType: 0 }
    const currentPrintType = encodeURIComponent(
      JSON.stringify(printTypeList[index])
    )
    let paramString = {}

    // 新版本
    if (this.data.version) {
      const obj = Object.assign(this.data.selectObj, param)
      for (let key in obj) {
        if (Array.isArray(obj[key])) {
          obj[key] = obj[key].join(',')
        }
      }
      paramString = obj
    } else {
      if (pmtype === 1) {
        param.id = pmtypeId
      } else {
        typeListData.forEach((item) => {
          const name = item.param
          param[name] = item.search
        })
      }
      paramString = param
    }
    paramString = encodeURIComponent(JSON.stringify(paramString))
    const url = `/pages/workService/components/pdfImage/index?currentPrintType=${currentPrintType}&paramString=${paramString}`
    wx.navigateTo({
      url,
    })
  },
  /**
   * @see 获取打印类型列表
   */
  getPrintType() {
    wx.showNavigationBarLoading()
    const request = { fileProperty: '0' }
    wx.$API['workService/getPrintType'](request)
      .then((result) => {
        wx.hideNavigationBarLoading()
        if (result.data.length) {
          this.setData({ printTypeList: result.data })
        } else {
          wx.showModal({
            title: '提示',
            content: '您没有可以生成的电子成绩单',
            showCancel: false,
            success: (res) => {
              wx.navigateBack({
                delta: 1,
              })
            },
          })
        }
      })
      .catch((err) => {
        wx.hideNavigationBarLoading()
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 2000,
        })
      })
  },
  /**
   * @see 选择打印类型方法
   */
  bindPickerChange(e) {
    const { value } = e.detail
    this.setData({
      index: value,
    })
    this.getType()
  },
  /**
   * @see 跳转查看申请记录
   */
  goRecordList() {
    wx.navigateTo({
      url: '/pages/workService/components/fileAndReportCardRecord/index?type=电子成绩单',
    })
  },
  // 单选统一处理
  handleRadioChange({ detail, currentTarget }) {
    const id = currentTarget.dataset.id
    const key = `selectObj.${id}`
    this.setData({
      [key]: detail.value,
    })
  },
  // 多选统一处理
  handleCheckboxChange({ detail, currentTarget }) {
    const id = currentTarget.dataset.id
    const key = `selectObj.${id}`
    let name = this.data.selectObj[id]
    name || (name = [])
    let index = name.indexOf(detail.value)
    index === -1 ? name.push(detail.value) : name.splice(index, 1)
    this.setData({
      [key]: name,
    })
  },
  // 表格统一处理
  handleTableChange({ detail }) {
    const id = detail.tableId
    const list = detail.list
    const key = `selectObj.${id}`
    this.setData({
      [key]: list.filter((item) => item.select).map((i) => i.value),
    })
  },
})
